﻿//_______________________________________________________________________________________________________________________
// Copy Right (c) 2010, 2011 SapiensIT Consulting GmbH, all rights reserved
//_______________________________________________________________________________________________________________________
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT
// NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
//_______________________________________________________________________________________________________________________
// PROJECT:     sapiens.at.SharePoint (SharePoint 2010/V4)
//_______________________________________________________________________________________________________________________
// VERSION:     2.0
//_______________________________________________________________________________________________________________________
// DESCRIPTION: 
//_______________________________________________________________________________________________________________________
// LICENSE:     Different licens Types apply to different file types:
//               - Source code files are governed by the MICROSOFT PUBLIC LICENSE (Ms-PL)
//               - Binary files are governed by MSDN CODE GALLERY BINARY LICENSE
//               - Documentation files are governed by CREATIVE COMMONS ATTRIBUTION 3.0 LICENSE
//
//               The licenses can be found at the projects source location.
//               If you do not agree, don't use it!
// 
//_______________________________________________________________________________________________________________________
// Start SharePoint Branding, visit http://www.cliggin.com! Visit us on http://www.sapiens.at! Tell your friends! 
//_______________________________________________________________________________________________________________________
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace Sapiens.at.SharePoint.Receiver
{
    /// <summary>
    /// Base class with some convinient methods for feature receivers.
    /// </summary>
    public class BaseFeatureReceiver : SPFeatureReceiver
    {
        /// <summary>
        /// Gets or sets the receiver properties of this feature receiver.
        /// </summary>
        protected SPFeatureReceiverProperties Properties { get; set; }

        SPWeb _web;
        /// <summary>
        /// Gets the web of this feature.
        /// </summary>
        protected SPWeb Web
        {
            get
            {
                if (_web == null)
                {
                    switch (Properties.Definition.Scope)
                    {
                        case SPFeatureScope.Site:
                            _web = ((SPSite)Properties.Feature.Parent).RootWeb; break;
                        case SPFeatureScope.Web:
                            _web = (SPWeb)Properties.Feature.Parent; break;
                        default:
                            throw new NotImplementedException("Retrieving of SPWeb for this scope is not implemented.");
                    }
                }
                return _web;
            }
        }

        SPSite _site;
        /// <summary>
        /// Gets the site of this feature.
        /// </summary>
        protected SPSite Site
        {
            get
            {
                if (_site == null)
                {
                    switch (Properties.Definition.Scope)
                    {
                        case SPFeatureScope.Site:
                            _site = (SPSite)Properties.Feature.Parent; break;
                        case SPFeatureScope.Web:
                            _site = ((SPWeb)Properties.Feature.Parent).Site; break;
                        default:
                            throw new NotImplementedException("Retrieving of SPWeb for this scope is not implemented.");
                    }
                }
                return _site;
            }
        }

        SPWebService _webservice;
        /// <summary>
        /// WebService that contains the WebApplications
        /// Use this in farm scoped features
        /// </summary>
        protected SPWebService WebService
        {
            get
            {
                if (_webservice == null)
                {
                    switch (Properties.Definition.Scope)
                    {
                        case SPFeatureScope.Farm:
                            _webservice = (SPWebService)Properties.Feature.Parent; break;
                        case SPFeatureScope.Site:
                            _webservice = ((SPSite)Properties.Feature.Parent).WebApplication.WebService; break;
                        case SPFeatureScope.Web:
                            _webservice = ((SPWeb)Properties.Feature.Parent).Site.WebApplication.WebService; break;
                        default:
                            throw new NotImplementedException("Retrieving of SPWeb for this scope is not implemented.");
                    }
                }
                return _webservice;
            }
        }

        SPWebApplication _webapplication;
        /// <summary>
        /// Gets the web application of this feature.
        /// </summary>
        protected SPWebApplication Webapplication
        {
            get
            {
                if (_webapplication == null)
                {
                    switch (Properties.Definition.Scope)
                    {
                        case SPFeatureScope.WebApplication:
                            _webapplication = (SPWebApplication)Properties.Feature.Parent; break;
                        case SPFeatureScope.Site:
                            _webapplication = ((SPSite)Properties.Feature.Parent).WebApplication; break;
                        case SPFeatureScope.Web:
                            _webapplication = ((SPWeb)Properties.Feature.Parent).Site.WebApplication; break;
                        default:
                            throw new NotImplementedException("Retrieving of SPWebApplication for this scope is not implemented.");
                    }
                }
                return _webapplication;
            }
        }

        /// <summary>
        /// Initializes the receiver.
        /// </summary>
        /// <param name="properties"></param>
        protected virtual void InitReceiver(SPFeatureReceiverProperties properties)
        {
            Properties = properties;

            // problem when accessing the properties Web and Site, that are already set with a non functionable instance.
            _site = null;
            _web = null;
        }

        #region SPFeatureReceiver Methods

        /// <summary>
        /// Overrides the standard event handler to initialize the receiver. If overridden call base.FeatureActivated!
        /// </summary>
        /// <param name="properties"></param>
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            InitReceiver(properties);
        }

        /// <summary>
        /// Overrides the standard event handler to initialize the receiver. If overridden call base.FeatureDeactivating!
        /// </summary>
        /// <param name="properties"></param>
        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            InitReceiver(properties);
        }

        /// <summary>
        /// Overrides the standard event handler to initialize the receiver. If overridden call base.FeatureInstalled!
        /// </summary>
        /// <param name="properties"></param>
        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {
            InitReceiver(properties);
        }

        /// <summary>
        /// Overrides the standard event handler to initialize the receiver. If overridden call base.FeatureUnistalling!
        /// </summary>
        /// <param name="properties"></param>
        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {
            InitReceiver(properties);
        }

        #endregion
    }
}
